﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SusyGame_Model
{
    public class Map
    {
        //Atributos
        private String mapName;
        private int mapType;
        private int[,] mapV;
        private int alto, ancho;
        private List<Texture2D> tileTextures = new List<Texture2D>();
        private Queue<Vector2> waypoints = new Queue<Vector2>();

        //Propiedades
        public String MapName
        {
            get { return mapName; }
            set { mapName = value; }
        }
        
        public int MapType
        {
            get { return mapType; }
            set { mapType = value; }
        }
        
        public int[,] MapV
        {
            get { return mapV; }
            set { mapV = value; }
        }
       
        public int Ancho
        {
            get { return ancho; }
            set { ancho = value; }
        }

        public int Alto
        {
            get { return alto; }
            set { alto = value; }
        }

        public void AddTexture(Texture2D texture)
        {
            tileTextures.Add(texture);
        }

        public Queue<Vector2> Waypoints
        {
            get { return waypoints; }
            set { waypoints = value; }
        }

        //Constructores
        public Map()
        {
        }

        public Map(int mapType)
        {
            //Las dimensiones del mapa seran fijas
            this.MapType = mapType;
            this.Alto = 9;
            this.Ancho = 8;

            //Creamos los tipos de mapas que tendremos
            Ground ground = new Ground();
            Pool pool = new Pool();

            this.MapV = new int[alto, ancho];

            //LLena todo con jardin
            for (int x = 0; x < alto; x++)
                for (int y = 0; y < ancho; y++)
                    mapV[x, y] = ground.Icon;

            if (mapType == 1)
            {//Mapa sin agua
                this.MapName = "Mapa sin agua";                                                  
            }
            else
            {
                //Crea el mapa jardin con piscina
                this.MapName = "Mapa con agua";
                
                //Dividimos el mapa para que se llene con agua
                int aAlto = (int)(alto / 3);
                int bAlto = (int)(alto * 2 / 3);

                //LLena el jardin con agua
                for (int x = aAlto; x < bAlto; x++)               
                    for (int y = 0; y < ancho; y++)                    
                        mapV[x, y] = pool.Icon;                   
            }

            waypoints.Enqueue(new Vector2(8, 0)*150);
            waypoints.Enqueue(new Vector2(0, 0)*150);
        }

        //Metodo para imprimir el mapa
        public void PrintMap(SpriteBatch batch)
        {
            for (int x = 0; x < Ancho; x++)
            {
                for (int y = 0; y < Alto; y++)
                {
                    int textureIndex = mapV[y, x];
                    if (textureIndex == -1)
                        continue;

                    Texture2D texture = tileTextures[textureIndex];
                    batch.Draw(texture, new Rectangle(
                        x * 150, y * 80, 150, 80), Color.White);
                }
            }        
        }

        public int GetIndex(int cellX, int cellY)
        {
            if (cellX < 0 || cellX > this.ancho || cellY < 0 || cellY > this.alto)
                return 0;

            return mapV[cellY, cellX];
        }
    } 	
}
